﻿/**
 * ...
 * @author Chase Kernan
 */

package com.chasekernan.hxnova.client;

import com.chasekernan.hxnova.client.mapviewer.IMapObject;
import com.chasekernan.hxnova.client.mapviewer.StarObject;
import com.chasekernan.hxnova.client.mapviewer.FleetObject;
import com.chasekernan.hxnova.client.mapviewer.SalvageObject;
import com.chasekernan.hxnova.core.dataholders.Global;
import com.chasekernan.hxnova.core.players.Player;

class SelectionFilter {
    
    
    public static var DEFAULT   = new SelectionFilter();
    public static var ALL       = {
        var sf = new SelectionFilter();
        
        sf.stars    = true;
        sf.fleets   = true;
        sf.salvage  = true;
        sf.enemies  = true;
        sf.friends  = true;
        sf.neutrals = true;
        sf.self     = true;
        
        sf;
    };
    
    public static var SELF_ONLY = {
        var sf = new SelectionFilter();
        
        sf.stars    = true;
        sf.fleets   = true;
        sf.salvage  = true;
        sf.enemies  = false;
        sf.friends  = false;
        sf.neutrals = false;
        sf.self     = true;
        
        sf;
    }
    
    public var stars    : Bool;
    public var fleets   : Bool;
    public var salvage  : Bool;
    public var enemies  : Bool;
    public var friends  : Bool;
    public var neutrals : Bool;
    public var self     : Bool;
    
    public var customs  : List<IMapObject -> Bool>;
    
    public function new() {
        stars    = true;
        fleets   = true;
        salvage  = false;
        enemies  = true;
        friends  = true;
        neutrals = true;
        self     = true;
        
        customs  = new List();
    }
    
    public function apply() {
        var customList = Lambda.list(Selection.getSelected());
        for (custom in customs) customList = Lambda.filter(customList, custom);
        
        var finalSelection = new Array<IMapObject>();
        
        for (obj in Lambda.array(customList)) {
            if (obj == null) continue;
            
            untyped switch (Type.getClass(obj)) {
                
                case StarObject:
                    if (!stars) continue;
                    if (!checkRelations(obj.getObject().owner)) continue;
                    
                case FleetObject:   
                    if (!fleets) continue;
                    if (!checkRelations(obj.getObject().owner)) continue;
                    
                case SalvageObject: 
                    if (!salvage) continue;
                
                default: 
            }
            
            finalSelection.push(obj);
        }
        
        Selection.setSelection(finalSelection);
    }
    
    private function checkRelations(other : Player) : Bool {
        if (other == null || other.id == -1) return false;
        
        var player = Global.player;
        
        if (other.id == player.id) return self;
        if (player.relations.isEnemyOf(other) && !enemies) return false;
        if (player.relations.isFriendOf(other) && !friends) return false;    
        if (player.relations.isNuetralTo(other) && !neutrals) return false;
            
        return true;
    }
    
}